package com.example.tests;

import static com.example.tests.ContactDataGenerator.loadContactsFromCsvFile;
import static com.example.tests.ContactDataGenerator.loadContactsFromXmlFile;
import static org.junit.Assert.assertThat;
import static org.hamcrest.Matchers.*;

import java.io.File;
import java.io.IOException;
import java.util.Iterator;

import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
import com.example.utils.SortedListOf;

public class ContactCreationTests extends TestBase {

	@DataProvider
  public Iterator<Object[]> contactsFromFile() throws IOException{
  	return wrapContactDataForDataProvider(loadContactsFromXmlFile(new File("contacts.xml"))).iterator();
  }

@Test(dataProvider = "contactsFromFile")
  public void testNonEmptyContactCreation(ContactData contact) throws Exception {
  	
	 	// save current list of contacts
//	 	SortedListOf<ContactData> oldList = app.getContactHelper().getContats();
	 	SortedListOf<ContactData> oldList = app.getModelContacts().getContacts();
	
  	// actions
	 	app.getContactHelper().createContact(contact);
    
    // save new state
//  	SortedListOf<ContactData> newList = app.getContactHelper().getContats();
  	SortedListOf<ContactData> newList = app.getModelContacts().getContacts();
	 	
	 	// compare collections
	  assertThat(newList, equalTo(oldList));
	  
    // compare model to implementation
    if (wantToCheck()) {
      if ("yes".equals(app.getProperty("check.db"))) {
        assertThat(app.getModelContacts().getContacts(), equalTo(app.getHibernateHelper().listContacts()));
        System.out.println("DB check done. On contact: " + contact.getFirstName());
      }
      
      if ("yes".equals(app.getProperty("check.ui"))) {
        assertThat(app.getModelContacts().getContacts(), equalTo(app.getContactHelper().getUiContacts()));
        System.out.println("UI check done. On contact: " + contact.getFirstName());
      }
    }
  }

}
